import Nifi from "../nifi";

// flow 里基本都是全局属性，理论上初始化之后就不需要变更了


interface Flow {
    disconnectedNodeAcknowledged: boolean;
    clientId: string;
    processorTypes: any[];
    controllerServiceTypes: any[];
    reportingTaskTypes: any[];
    parameterProviderTypes: any[];
    prioritizerTypes: any[];
    parameterContexts: any[];
    copyCells: {};
    refresh: () => void
    refreshParameterContexts: () => Promise<void>
    refreshControllerServiceTypes:() => Promise<void>
}

export default (nifi: Nifi) => {
    const getClientId = async () => await nifi.api.get('/nifi-api/flow/client-id',{type:'text'});
    const getProcessorTypes = async () => (await nifi.api.get('/nifi-api/flow/processor-types')).processorTypes;
    const getControllerServiceTypes = async () => (await nifi.api.get('/nifi-api/flow/controller-service-types')).controllerServiceTypes;
    const getReportingTaskTypes = async () => (await nifi.api.get('/nifi-api/flow/reporting-task-types')).reportingTaskTypes;
    const getParameterProviderTypes = async () => (await nifi.api.get('/nifi-api/flow/parameter-provider-types')).parameterProviderTypes;
    const getPrioritizerTypes = async () => (await nifi.api.get('/nifi-api/flow/prioritizers')).prioritizerTypes;
    const getParameterContexts = async () => (await nifi.api.get('/nifi-api/flow/parameter-contexts')).parameterContexts;
    //
    const initFlow = async () => {
        const [clientId, processorTypes, controllerServiceTypes, reportingTaskTypes, parameterProviderTypes, prioritizerTypes, parameterContexts] = await Promise.all([getClientId(), getProcessorTypes(), getControllerServiceTypes(), getReportingTaskTypes(), getParameterProviderTypes(), getPrioritizerTypes(), getParameterContexts()])
        flow.clientId = clientId;
        flow.processorTypes = processorTypes;
        flow.controllerServiceTypes = controllerServiceTypes;
        flow.reportingTaskTypes = reportingTaskTypes;
        flow.parameterProviderTypes = parameterProviderTypes;
        flow.prioritizerTypes = prioritizerTypes;
        flow.parameterContexts = parameterContexts;
    }

    // 刷新 parameter Context
    const refreshParameterContexts = async () => {
        const res = await getParameterContexts();
        flow.parameterContexts = res;
    }
    // 刷新控制器服务类型列表
    const refreshControllerServiceTypes = async () => {
        const res = await getControllerServiceTypes();
        flow.controllerServiceTypes = res;
    }

    const flow: Flow = { disconnectedNodeAcknowledged: false, clientId: "", processorTypes: [], controllerServiceTypes: [], reportingTaskTypes: [], parameterProviderTypes: [], prioritizerTypes: [], parameterContexts: [], copyCells: {}, refresh: initFlow ,refreshParameterContexts,refreshControllerServiceTypes}
    initFlow();
    //

    return flow
}